package com.hust360.percent.activity;

import java.lang.ref.WeakReference;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.hust360.percent.PercentApplication;
import com.hust360.percent.R;
import com.hust360.percent.pojo.Discount;
import com.hust360.percent.pojo.Discount.DiscountDAO;
import com.hust360.percent.pojo.Image;
import com.hust360.percent.pojo.Shop;
import com.hust360.percent.pojo.Shop.ShopDAO;
import com.hust360.percent.utils.ActivityUtils;

public class ShopDetailActivity extends Activity {

	/**
	 * TAG FOR LOGCAT OF DDMS
	 */
	public static final String TAG = "ShopDetilActivity";

	PercentApplication application;

	ShopDAO shopDAO;

	DiscountDAO discountInfoDAO;
	
	/**
	 * 左上角的返回按钮
	 */
	ImageView backBtn;

	/**
	 * tab上的评价按钮
	 */
	TextView appraiseBtn;

	/**
	 * tab上的活动信息按钮
	 */
	TextView discountInfoBtn;

	/**
	 * tab上的足迹按钮
	 */
	TextView footprintBtn;

	/**
	 * 除去tab之外，下方的ViewPager
	 */
	ViewPager viewPager;

	/**
	 * 显示图片的ViewPager
	 */
	ViewPager imgViewPager;

	/**
	 * ViewPager 的适配器
	 */
	SpDtPagerAdapter adapter;

	/**
	 * 评论tab的view的根
	 */
	LinearLayout appraise_root;

	/**
	 * 店铺总评分的RatingBar指示器
	 */
	RatingBar totalRating;

	/**
	 * 评论列表
	 */
	ListView appraiseList;

	/**
	 * 用户输入评论的输入框
	 */
	EditText appraiseEditText;

	/**
	 * 用户自己评论的框框
	 */
	RatingBar mRatingBar;

	/**
	 * 活动信息的view的根
	 */
	LinearLayout discount_root;

	/**
	 * 店铺名字
	 */
	TextView shopNameTextView;

	/**
	 * 活动文本描述
	 */
	TextView contentTextView;

	/**
	 * 活动地址
	 */
	TextView addressTextView;
	
	/**
	 * 店铺评价总分数
	 */
	RatingBar shopRating;
	
	/**
	 * 显示分数的文本
	 */
	TextView shopRatingTV;

	/**
	 * 电话号码
	 */
	TextView phoneTextView;

	/**
	 * 收藏的人数
	 */
	TextView likedTextView;

	/**
	 * 收藏按钮
	 */
	TextView addToFavor;

	/**
	 * 举报按钮
	 */
	TextView reportBtn;

	/**
	 * 致电按钮
	 */
	Button callButton;

	/**
	 * 定位按钮
	 */
	Button locateButton;

	/**
	 * 分享按钮
	 */
	Button sharedButton;

	/**
	 * 足迹tab的根
	 */
	LinearLayout footprint_root;

	/**
	 * Intent传过来的DiscountInfo类型的实例
	 */
	Discount discountInfo;

	/*--------------------------多线程相关--------------------------------------------*/
	/**
	 * 多线程处理消息的handler
	 */
	Handler handler;

	/**
	 * 线程对象
	 */
	HandlerThread thread;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ActivityUtils.addActivity(this);
		setContentView(R.layout.activity_info_detil);

		application = (PercentApplication) getApplication();
		shopDAO = application.getShopDao();
		discountInfoDAO = application.getDiscountInfoDAO();

		inflateViews();
		findAllViewsById();
		
		//initData();
		//将加载资源的操作交给子线程运行
		Thread bgThread = new  Thread(new LoadTextRunnable());
		bgThread.start();
		
		adapter = new SpDtPagerAdapter();
		viewPager.setAdapter(adapter);

		// 设置默认界面是第二个
		viewPager.setCurrentItem(1);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		ActivityUtils.removeActivity(this);
	}

	/**
	 * 通过findViewById获取界面中的
	 */
	void findAllViewsById() {
		viewPager = (ViewPager) findViewById(R.activity_info_detil.viewpager);
		appraiseBtn = (TextView) findViewById(R.activity_info_detil.appraiseBtn);
		discountInfoBtn = (TextView) findViewById(R.activity_info_detil.discountInfoBtn);
		footprintBtn = (TextView) findViewById(R.activity_info_detil.footprintBtn);
		TabItemClickListener listener = new TabItemClickListener();
		appraiseBtn.setOnClickListener(listener);
		discountInfoBtn.setOnClickListener(listener);
		footprintBtn.setOnClickListener(listener);
		imgViewPager = (ViewPager) discount_root
				.findViewById(R.detail_info_pager_item.horizontalListView);
		
		backBtn = (ImageButton)findViewById(R.activity_info_detil.back);
		backBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});

		/*----------------------*/
		appraiseList = (ListView) appraise_root
				.findViewById(R.appraise.listview);
		appraiseEditText = (EditText) appraise_root
				.findViewById(R.appraise.content_edit_text);
		totalRating = (RatingBar) appraise_root
				.findViewById(R.appraise.shop_appraise_indicator);
		mRatingBar = (RatingBar) appraise_root
				.findViewById(R.appraise.ratingbar);

		/*------------------------*/
		shopNameTextView = (TextView) discount_root
				.findViewById(R.detail_info_pager_item.title);
		contentTextView = (TextView) discount_root
				.findViewById(R.detail_info_pager_item.content);
		addressTextView = (TextView) discount_root
				.findViewById(R.detail_info_pager_item.address);
		phoneTextView = (TextView) discount_root
				.findViewById(R.detail_info_pager_item.telephone);
		likedTextView = (TextView) discount_root
				.findViewById(R.detail_info_pager_item.liked);
		addToFavor = (TextView) discount_root
				.findViewById(R.detail_info_pager_item.add_to_favor);
		reportBtn = (TextView) discount_root
				.findViewById(R.detail_info_pager_item.report_btn);
		callButton = (Button) discount_root
				.findViewById(R.detail_info_pager_item.call);
		locateButton = (Button) discount_root
				.findViewById(R.detail_info_pager_item.locate);
		sharedButton = (Button) discount_root
				.findViewById(R.detail_info_pager_item.shared);
		shopRating = (RatingBar) discount_root.findViewById(R.detail_info_pager_item.ratingBar);
		shopRatingTV = (TextView)discount_root.findViewById(R.detail_info_pager_item.rating_point);
		/*---------------------------------------*/

	}

	/**
	 * 通过LayoutInflater对象从XML加载控件
	 */
	private void inflateViews() {
		LayoutInflater inflater = (LayoutInflater) getLayoutInflater();
		appraise_root = (LinearLayout) inflater
				.inflate(R.layout.appraise, null);
		discount_root = (LinearLayout) inflater.inflate(
				R.layout.detail_info_pager_item, null);
		footprint_root = (LinearLayout) inflater.inflate(R.layout.footprint,
				null);
	}

	/**
	 * 初始化线程对象
	 */
	@SuppressWarnings("unused")
	private void initThread() {
		thread = new HandlerThread("ShopDetilThread");
		thread.start();
		handler = new SpDtHandler(this, thread.getLooper());
	}

	/**
	 * 初始化视图的数据
	 */
	private void initData() {
		Intent intent = getIntent();
		String TAG = intent.getStringExtra("TAG");
		if (TAG != null && TAG.equals(BoutiqueActivity.TAG)) {
			discountInfo = (Discount) intent
					.getSerializableExtra("DiscountInfo");
			if (discountInfo != null) {
				
				/*------店铺信息-----*/
				Shop shop = discountInfo.getShop();
				shopNameTextView.setText(shop.name);
				contentTextView.setText(discountInfo.describe);
				addressTextView.setText(shop.address);
				phoneTextView.setText(shop.telephone);
				likedTextView.setText("" + shop.getLiked());
				shopRating.setRating(discountInfo.getRate());
				shopRatingTV.setText(discountInfo.getRate() + "");
				
				/*------评价页------*/
				totalRating.setRating(discountInfo.getRate());
			}
		}
	}

	/**
	 * Shop Detil Handler.
	 */
	private static class SpDtHandler extends Handler {

		/**
		 * 当评价页面的list view的内容发生改变时的消息
		 */
		public static final int MESSAGE_LIST_DATA_CHANGE = 1;

		public SpDtHandler(Activity activity, Looper looper) {
			super(looper);
			reference = new WeakReference<ShopDetailActivity>(
					(ShopDetailActivity) activity);
		}

		@SuppressWarnings("unused")
		WeakReference<ShopDetailActivity> reference;

		@Override
		public void handleMessage(Message msg) {
			switch (msg.arg1) {
			case MESSAGE_LIST_DATA_CHANGE:
				// activity.runOnUiThread(null);
				break;

			default:
				break;
			}
		}

	}

	private class SpDtPagerAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			return 3; // 总共只有3个面板，对应tab上面的3个按钮
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
			// 从写改方法，不让系统回收ViewPager里面的3个页面的资源
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			switch (position) {
			case 0:
				container.addView(appraise_root);
				return appraise_root;
			case 1:
				container.addView(discount_root);
				return discount_root;
			case 2:
				container.addView(footprint_root);
				return footprint_root;
			default:
				Log.e(TAG,
						"shop detail activity view pager adapter instantiateItem out of index");
				break;
			}
			return null;
		}

	}

	private class TabItemClickListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.activity_info_detil.appraiseBtn:
				viewPager.setCurrentItem(0);
				break;
			case R.activity_info_detil.discountInfoBtn:
				viewPager.setCurrentItem(1);
				break;
			case R.activity_info_detil.footprintBtn:
				viewPager.setCurrentItem(2);
				break;
			default:
				break;
			}
		}
	}

	@SuppressWarnings("unused")
	private class ImageViewPagerAdapter extends PagerAdapter {

		List<Image> imageList;
		Drawable defaultImg;

		public ImageViewPagerAdapter() {
			super();
			imageList = discountInfo.getImages();
			defaultImg = getResources().getDrawable(R.drawable.logo2);
		}

		@Override
		public int getCount() {
			return imageList == null ? 0 : imageList.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			ImageView imageView = new ImageView(ShopDetailActivity.this);
			return super.instantiateItem(container, position);
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			super.destroyItem(container, position, object);
		}

		@Override
		public float getPageWidth(int position) {
			
			return super.getPageWidth(position);
		}
	}
	
	/**
	 * 异步加载文字的Runnable接口的实现类
	 * @author yangyang
	 *
	 */
	private class LoadTextRunnable implements Runnable {

		@Override
		public void run() {
			initData();
			Thread.currentThread().interrupt();
		}
	}
}
